package com.blb.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.blb.entity.Role;
import com.blb.entity.User;
import com.blb.service.RoleService;
import com.blb.utils.DateUtils;
import com.blb.utils.LayuiPage;
import com.blb.utils.UUIDUtils;

@Controller
@RequestMapping("role")
public class RoleController {

	private RoleService roleService = new RoleService();
	
	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param rolename
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("page")
	public LayuiPage page(int page,int limit,String rolename) throws IOException{
		LayuiPage pagelist = roleService.page((page-1)*limit, limit, rolename);
		return pagelist;
	}
	
	@ResponseBody
	@RequestMapping("isExist")
	public String isExist(String rolename){
		try {
			List<Role> list = roleService.isExist(rolename);
			if(!list.isEmpty()){
				return "角色已存在";
			}
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}
	
	
	/**
	 * 角色添加
	 * @param role
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public String add(Role role,HttpSession session){
		role.setId(UUIDUtils.getID());
		role.setCreateTime(DateUtils.gettime());
		
		User user = (User)session.getAttribute("user");
		role.setCreateUser(user.getUsername());
		try {
			roleService.add(role);
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}
	
	@InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

	
	/**
	 * 修改角色
	 * @param role
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public String update(Role role,HttpSession session){
		role.setUpdateTime(DateUtils.gettime());
		User user = (User) session.getAttribute("user");
		role.setUpdateUser(user.getUsername());
		System.out.println(role.getRolename());
		System.out.println(role.getNote());
		try {
			roleService.update(role);
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}
	
	/**
	 * 删除角色
	 * @param rolesIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(@RequestBody List<String> rolesIds){
		try {
			roleService.delete(rolesIds);
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}
}
